﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace LinqToMetaDataAddIn
{
    public partial class LinqToMetaDataWizForm : Form
    {
        private string _dbmlFile;
      
        public string TableToElaborate;
        public bool OneFileForOneClass;
        public bool ReplaceExtingFile;
        public string DateFormat;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbmlFile"></param>
        public LinqToMetaDataWizForm(string dbmlFile)
        {
            InitializeComponent();

            this._dbmlFile = dbmlFile;
          

            this.LoadData();
        }

        private void LoadData()
        {
            StreamReader sr = new StreamReader(this._dbmlFile);
            string inputFileContent = sr.ReadToEnd();
            sr.Close();

            XElement database = XElement.Parse(inputFileContent);
            XNamespace xNS = database.Name.Namespace;

            var tables = database.Elements(xNS + "Table");

            this.cboTables.Items.Add("(All Tables)");

            foreach (XElement table in tables)
                this.cboTables.Items.Add((string)table.Attribute("Member"));

            this.cboTables.SelectedIndex = 0;

            DataTable dtDateFormat = new DataTable();
            dtDateFormat.Columns.Add("Key");
            dtDateFormat.Columns.Add("Value");

            dtDateFormat.Rows.Add( "{0:d}", "01/07/1972");
            dtDateFormat.Rows.Add("{0:D}", "venerdì 01 luglio 1972");
            dtDateFormat.Rows.Add("{0:dd-MMM-yyyy}", "01 lug 1972");

            this.cboDateFormat.ValueMember = "Key";
            this.cboDateFormat.DisplayMember = "Value";
            this.cboDateFormat.DataSource = dtDateFormat;
        }

        private void cboTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool b = (this.cboTables.Text == "(All Tables)");

            this.rb1FileFor1Class.Enabled = b;
            this.rb1FileForAllClass.Enabled = b;

            this.rb1FileFor1Class.Checked = b;
            this.rb1FileForAllClass.Checked = !b;
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            this.OneFileForOneClass = this.rb1FileFor1Class.Checked;

            if (this.cboTables.Text != "(All Tables)")
            { this.TableToElaborate = this.cboTables.Text; }

            this.DateFormat = this.cboDateFormat.SelectedValue.ToString();
            this.ReplaceExtingFile = this.ckReplaceFile.Checked;
             
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void brnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }


    }
}
